package com.school.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.school.domain.DirectLog;
import com.school.domain.Product;
import com.school.extra.base.BaseController;
import com.school.extra.pojo.DirectLogVo;
import com.school.mapper.DirectLogMapper;
import com.school.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import zhibi.frame.mybatis.example.Example;
import zhibi.utils.net.HttpUtils;
import zhibi.utils.request.ParamUtils;
import zhibi.utils.security.SecurityUtil;

import java.io.UnsupportedEncodingException;
import java.util.Date;

@Controller
@RequestMapping("direct")
public class DirectLogController extends BaseController {


    @Autowired
    private DirectLogMapper directLogMapper;
    @Autowired
    private ProductMapper productMapper;

    /**
     * 列表
     *
     * @param directLog
     * @param pageNum
     * @param model
     * @return
     */
    @RequestMapping("list")
    public String list(DirectLog directLog, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        model.addAttribute("tipMsg", session.getAttribute("tipMsg"));
        session.removeAttribute("tipMsg");
        Example example = Example.getInstance();
        example.addParam("productid", directLog.getProductid());
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<DirectLogVo> pageInfo = new PageInfo<DirectLogVo>(directLogMapper.selectVoByExample(example), 5);
        model.addAttribute(pageInfo);
        model.addAttribute("productid", directLog.getProductid());
        model.addAttribute("url", request.getRequestURI() + "?" + ParamUtils.params2String(request));
        return "direct/list";
    }


    /**
     * @return
     */
    @RequestMapping("add/{productid}")
    public String add(String opt, String opt2, Integer ordertime, Integer wordtime, @PathVariable Integer productid) throws UnsupportedEncodingException {
        Product product = productMapper.selectByPrimaryKey(productid);
        DirectLog directLog = new DirectLog();
        String data = ordertime + "_" + wordtime + "_" + opt.hashCode() + "_" + opt2.hashCode() + "_" + productid;
        data = data + "_" + SecurityUtil.HmacMd5(data, data);
        directLog.setData(data);
        directLog.setOperation(opt);
        directLog.setProductid(productid);
        directLog.setAddtime(new Date());
        directLogMapper.insert(directLog);
        sendOpt(product.getIphost(), data);
        session.setAttribute("tipMsg", "发送成功");
        return "redirect:/direct/list?productid=" + productid;
    }

    /**
     * 发送指令
     *
     * @param ipHost
     * @param data
     */
    private void sendOpt(String ipHost, String data) {
        new Thread(() -> {
            HttpUtils.post(ipHost, data);
        });
    }

}
